package com.xuyiwei.xblog.modular.article.controller;

import com.xuyiwei.xblog.business.article.model.ArticleLook;
import com.xuyiwei.xblog.business.article.service.IArticleLookService;
import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.util.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.xuyiwei.xblog.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

/**
 * 文章浏览控制器
 *
 * @author fengshuonan
 * @Date 2018-10-29 17:00:35
 */
@Controller
@RequestMapping("/article/articleLook")
public class ArticleLookController extends BaseController {

    private String PREFIX = "/article/articleLook/";

    @Autowired
    private IArticleLookService articleLookService;

    /**
     * 跳转到文章浏览首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "articleLook.html";
    }

    /**
     * 跳转到添加文章浏览
     */
    @RequestMapping("/articleLook_add")
    public String articleLookAdd() {
        return PREFIX + "articleLook_add.html";
    }

    /**
     * 跳转到修改文章浏览
     */
    @RequestMapping("/articleLook_update/{articleLookId}")
    public String articleLookUpdate(@PathVariable Integer articleLookId, Model model) {
        ArticleLook articleLook = articleLookService.selectById(articleLookId);
        model.addAttribute("item",articleLook);
        LogObjectHolder.me().set(articleLook);
        return PREFIX + "articleLook_edit.html";
    }

    /**
     * 获取文章浏览列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result<List<ArticleLook>> list(String condition) {
        Result<List<ArticleLook>> result = Result.instance();
        try{
            List<ArticleLook> list = articleLookService.selectList(null);
            result.setData(list);
        }catch (Exception e){
            result.setError("500","获取文章标签出错");
        }
        return result;
    }

    /**
     * 新增文章浏览
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result<Boolean> add(ArticleLook articleLook) {
        Result<Boolean> result = Result.instance();
        try{
            articleLookService.insert(articleLook);
            result.setData(true);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 删除文章浏览
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(required = true, value = "ids[]") List<Integer> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.articleLookService.deleteBatchIds(ids);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 修改文章浏览
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result<Boolean> update(ArticleLook articleLook) {
        Result<Boolean> result = Result.instance();
        try{
            articleLookService.updateById(articleLook);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 文章浏览详情
     */
    @RequestMapping(value = "/detail/{articleLookId}")
    @ResponseBody
    public Result<ArticleLook> detail(@PathVariable("articleLookId") Integer articleLookId) {
        Result<ArticleLook> result = Result.instance();
        try{
            ArticleLook articleLook = articleLookService.selectById(articleLookId);
            result.setData(articleLook);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }
}
